import {
  userJobType,
  userJobInteractionList,
  userJobInteractionUpdate,
  updateUserInfo,
  majorList
} from './index';
// import home06 from "@/static/images/tab/home-06.png";
// import home10 from "@/static/images/tab/home-10.png";
const home06 = 'https://yiliao.test03.qcw800.com/static/images/tab/home-06.png'
const home10 = 'https://yiliao.test03.qcw800.com/static/images/tab/home-10.png'

export interface CommonJobTypeParams {
  id : number;
  name : string;
}
export interface CommonJobTypeResult {
  titleList : CommonJobTypeParams[];
}
/**
 * 通用-职位分类
 * @param {string} fromContent 来源内容(打印日志用)
 * @return {any} {
   * titleList:[] //职位分类二级标题
   * }
 */
export async function commonGetJobType(fromContent : string = "未知") : Promise<any> {
  console.log(fromContent, ' 职位分类 不需要参数');
  let resultObj : CommonJobTypeResult = {
    titleList: []
  }
  let result = await userJobType();
  console.log(fromContent, ' 用户-职位分类结果 r::', result);
  for (let i = 0; i < result.length; i++) {
    const r = result[i]
    if (r.level == 2) {
      let rr : CommonJobTypeParams = {
        id: r.id,
        name: r.content
      }
      resultObj.titleList.push(rr)
    }
  }
  console.log(fromContent, ' 用户-职位分类转义结果 r:::', resultObj);
  return resultObj;
}

export interface JobInteractionListParams {
  like ?: number;        //0 默认 1感兴趣 2不感兴趣（参数全部可选）
  look_me ?: number;     //HR看过用户
  me_look ?: number;     //用户看过HR
  talk ?: number;        //用户沟通过
  talk_hr ?: number;     //HR沟通过
  resume_send ?: number; //简历已投递
  like_hr ?: number;     //HR感兴趣
  hr_look ?: number;     //HR看过用户
  look_hr ?: number;     //用户看过HR
}

export interface JobInteractionResult {
  job_name ?: string;
  job_id ?: number;
  salary_min ?: number;
  salary_max ?: number;
  salary_count ?: number;
  province ?: string;
  city ?: string;
  job_nature ?: string;
  qualifications ?: string;
  highlights ?: string;
  company_id ?: string;
  company_logo ?: string;
  company_name ?: string;
  company_type ?: string;
  company_listed ?: string;
  like ?: number;
  like_img ?: string;
  hr_avatar ?: string;
  hr_name ?: string;
  hr_duty ?: string;
  hr_id ?: number;
  resume_send : number;
  hr_user_id : number;
}
/**
 * 通用-职位互动列表
 * @param {any} params  //like look_me me_look talk resume_send like_hr hr_look look_hr
 * @param {string} fromContent
 * @return {any} {
   * jobInteractionList:[{job_name:"", job_id:""...}] //职位互动列表
   * }
 */
export async function commonJobInteractionList(params : any, fromContent : string = "未知") : Promise<JobInteractionResult[]> {
  let resultObj : JobInteractionResult[] = []
  if (!params) {
    console.log(fromContent, '用户-互动列表 请求参数为空')
    return resultObj
  }
  try {
    let result = await userJobInteractionList(params);
    console.log(fromContent, ' 用户-互动列表 r:::', result);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!result) {
      console.log(fromContent, '用户-互动列表 返回值为空')
      return resultObj
    }
    for (let i = 0; i < result.list.length; i++) {
      const r = result.list[i]
      let rr : JobInteractionResult = {
        like: r.like,
        like_img: r.like == 1 ? home06 : home10,
        resume_send: r.resume_send,
        hr_user_id: r.hr_user_id,
      }
      if (r.job) {
        rr.job_name = r.job.name
        rr.job_id = r.job.id
        rr.salary_min = r.job.salary_min
        rr.salary_max = r.job.salary_max
        rr.salary_count = r.job.salary_count
        rr.province = r.job.province.name
        rr.city = r.job.city.name
        rr.job_nature = r.job.nature
        rr.qualifications = ''
        rr.highlights = r.job.highlights
        rr.company_id = r.job.company.id
        rr.company_logo = r.job.company.url_logo
        rr.company_name = r.job.company.name
        rr.company_type = r.job.company.type == 1 ? '民营' : r.job.company.type == 2 ? '国企' : '合资'
        rr.company_listed = r.job.company.listed == 1 ? '未上市' : '上市'
        rr.hr_avatar = r.job.user.avatar
        rr.hr_name = r.job.user.name
        rr.hr_duty = r.job.user.hr.duty.name
        rr.hr_id = r.job.user.id
        let qualifications = ''
        for (let j = 0; j < r.job.qualifications.length; j++) {
          qualifications += r.job.qualifications[j] + ' '
        }
        rr.qualifications = qualifications
      }
      resultObj.push(rr)
    }
    console.log(fromContent, ' 用户-互动列表转义结果 r:::', resultObj);
    return resultObj;
  } catch (e) {
    //TODO handle the exception
    console.log(fromContent, ' 用户-互动列表异常 err:::', e)
    return resultObj;
  }
}

export interface JobInteractionUpdateParams {
  job_id ?: number;
  like ?: number;
  resume_id ?: number;
  like_hr ?: number;
  talk ?: number;
  hr_user_id ?: number;
  talk_hr ?: number;
}
export async function jobInteractionUpdate(params : JobInteractionUpdateParams, fromContent = "未知") : Promise<boolean> {
  try {
    console.log(fromContent, ' 通用-互动更新 参数:::', params)
    let r = await userJobInteractionUpdate(params);
    console.log(fromContent, ' 通用-互动更新 结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r) {
      console.log(fromContent, '通用-互动更新 返回值为空')
      return false;
    }
    return true;
  } catch (e) {
    //TODO handle the exception
    console.log(fromContent, ' 通用-互动更新 异常 err:::', e)
    return false
  }
}

/**
 * 用户点击立即联系调用
 * @param {number} job_id //职位ID
 * @param {number} hr_user_id   //hr用户ID
 * @param {string} fromContent  //打印内容
 * @return
 */
export async function userChat(job_id : number, hr_user_id : number, fromContent : string = "用户立即沟通") : Promise<boolean> {
  try {
    let params : JobInteractionUpdateParams = {
      job_id: job_id,
      hr_user_id: hr_user_id,
      talk: 1
    }
    let r = await jobInteractionUpdate(params, fromContent);
    if (!r || !r.id) {
      return false
    }
    return true;
  } catch (e) {
    //TODO handle the exception
    console.log(fromContent, ' 通用-互动更新 异常 err:::', e)
    return false;
  }
}

export async function hrChat(resume_id : number, fromContent : string = "HR立即沟通") : Promise<boolean> {
  try {
    let params : JobInteractionUpdateParams = {
      resume_id: resume_id,
      talk_hr: 1
    }
    let r = await jobInteractionUpdate(params, fromContent);
    if (!r) {
      return false
    }
    return true;
  } catch (e) {
    //TODO handle the exception
    console.log(fromContent, ' 通用-互动更新 异常 err:::', e)
    return false;
  }
}

export interface UserInfoUpdateParams {
  name ?: string;
  email ?: string;
  avatar ?: string;
  gender ?: number;
  born ?: string;
  province_id ?: number;
  city_id ?: number;
  political_outlook ?: number;
  age ?: number;
  job_status ?: number;
  work_start_time ?: string;
  work_end_time ?: string;
  highlights ?: string;
}
export async function commonUserInfoUpdate(params : UserInfoUpdateParams, fromContent : string = "未知") : Promise<boolean> {
  try {
    console.log(fromContent, ' 通用-用户信息更新 参数:::', params)
    let r = await updateUserInfo(params);
    console.log(fromContent, ' 通用-用户信息更新 结果:::', r)
    if (!r) {
      return false
    }
    return true;
  } catch (e) {
    //TODO handle the exception
    console.log(fromContent, ' 通用-用户信息更新 异常 err:::', e)
    return false;
  }
}

export interface MajorListResult {
  "id" : number;
  "name" : string;
}
export async function commonMajorList(fromContent : string = "专业列表") : Promise<MajorListResult[]> {
  let resultObj : MajorListResult[] = []
  try {
    let r = await majorList();
    console.log(fromContent, ' 通用-专业列表 结果:::', r)
    if (!r) {
      return resultObj
    }
    r.forEach(item => {
      resultObj.push({
        id: item.id,
        name: item.name
      })
    })
    return resultObj;
  } catch (e) {
    //TODO handle the exception
    console.log(fromContent, ' 通用-用户信息更新 异常 err:::', e)
    return resultObj;
  }
}
